using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// UI组辅助器。
/// </summary>
public sealed class UIGroupHelper : MonoBehaviour, IUIGroupHelper
{
    public const int DepthFactor = 100;

    private int m_Depth = 0;
    private Canvas m_CachedCanvas = null;
    private CanvasScaler m_CachedCanvasScaler = null;
    private GraphicRaycaster m_CachedGraphicRaycaster = null;
    private RectTransform m_CachedRectTransform = null;

    private void Awake()
    {
        InitializeComponents();
    }

    private void Start()
    {
        SetupRectTransform();
    }

    /// <summary>
    /// 初始化组件。
    /// </summary>
    private void InitializeComponents()
    {
        // 获取或添加Canvas组件
        m_CachedCanvas = gameObject.GetOrAddComponent<Canvas>();
        m_CachedCanvas.overrideSorting = true;
        m_CachedCanvas.renderMode = RenderMode.ScreenSpaceOverlay;

        // 获取或添加CanvasScaler组件
        m_CachedCanvasScaler = gameObject.GetOrAddComponent<CanvasScaler>();
        if (m_CachedCanvasScaler != null)
        {
            m_CachedCanvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
            m_CachedCanvasScaler.referenceResolution = new Vector2(1920, 1080);
            m_CachedCanvasScaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
            m_CachedCanvasScaler.matchWidthOrHeight = 0.5f;
        }

        // 获取或添加GraphicRaycaster组件
        m_CachedGraphicRaycaster = gameObject.GetOrAddComponent<GraphicRaycaster>();

        // 获取RectTransform
        m_CachedRectTransform = GetComponent<RectTransform>();
        if (m_CachedRectTransform == null)
        {
            m_CachedRectTransform = gameObject.AddComponent<RectTransform>();
        }
    }

    /// <summary>
    /// 设置RectTransform属性。
    /// </summary>
    private void SetupRectTransform()
    {
        if (m_CachedRectTransform == null)
        {
            m_CachedRectTransform = GetComponent<RectTransform>();
            if (m_CachedRectTransform == null)
            {
                m_CachedRectTransform = gameObject.AddComponent<RectTransform>();
            }
        }

        // 设置为全屏
        m_CachedRectTransform.anchorMin = Vector2.zero;
        m_CachedRectTransform.anchorMax = Vector2.one;
        m_CachedRectTransform.anchoredPosition = Vector2.zero;
        m_CachedRectTransform.sizeDelta = Vector2.zero;
        m_CachedRectTransform.localPosition = Vector3.zero;
        m_CachedRectTransform.localRotation = Quaternion.identity;
        m_CachedRectTransform.localScale = Vector3.one;
    }

    /// <summary>
    /// 设置UI组深度。
    /// </summary>
    /// <param name="depth">UI组深度。</param>
    public void SetDepth(int depth)
    {
        m_Depth = depth;
        Canvas canvas = GetOrAddCanvas();
        if (canvas != null)
        {
            canvas.sortingOrder = DepthFactor * depth;
        }
    }

    /// <summary>
    /// 获取或添加Canvas组件。
    /// </summary>
    /// <returns>Canvas组件。</returns>
    private Canvas GetOrAddCanvas()
    {
        if (m_CachedCanvas == null)
        {
            m_CachedCanvas = gameObject.GetOrAddComponent<Canvas>();
            m_CachedCanvas.overrideSorting = true;
            m_CachedCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
        }

        return m_CachedCanvas;
    }

    /// <summary>
    /// 获取RectTransform。
    /// </summary>
    /// <returns>RectTransform组件。</returns>
    public RectTransform GetRectTransform()
    {
        if (m_CachedRectTransform == null)
        {
            m_CachedRectTransform = GetComponent<RectTransform>();
        }
        return m_CachedRectTransform;
    }
}

